package org.trc.traffic.analysis;

import java.util.HashMap;

import com.vividsolutions.jts.geom.Envelope;

public class AggCongestionCluster {
	
}

class AggLinkCongestionCluster extends AggCongestionCluster{
	public String clusterID = null;
		
	public int startTime = Integer.MAX_VALUE;
	public int endTime = Integer.MIN_VALUE;
	
	public int dur;
	
	public int num;

	public AggLinkCongestionCluster(String clusterID){
		this.clusterID  = clusterID;		
	}
	
	public void add(LinkCongestionCluster lcc) {		
		if(lcc.startTime < startTime)
			startTime = lcc.startTime;
		
		if(lcc.endTime > endTime)
			endTime = lcc.endTime;
		
		dur += lcc.dur;
		
		num++;
	}
}

class AggMLinkCongestionCluster extends AggCongestionCluster{
	public String clusterID = null;
	
	public int startTime = Integer.MAX_VALUE;
	public int endTime = Integer.MIN_VALUE;
	
	public float startOffset = Float.MAX_VALUE;	
	public float endOffset = Float.MIN_VALUE;
	
	public HashMap<String, LinkAggTraffic> linkTrafficSet = new HashMap<String, LinkAggTraffic>();
	
	public int dur;
	
	public int num;

	public AggMLinkCongestionCluster(String clusterID){
		this.clusterID  = clusterID;		
	}
	
	public void add(MLinkCongestionCluster mlcc) {
		this.startTime = mlcc.startTime < this.startTime ? mlcc.startTime : this.startTime;

		this.endTime = mlcc.endTime > this.endTime ? mlcc.endTime :  this.endTime;
			
		this.startOffset = mlcc.startOffset  < this.startOffset ? mlcc.startOffset : this.startOffset;
			
		this.endOffset = mlcc.endOffset > this.endOffset ? mlcc.endOffset : this.endOffset;
		
		dur += (mlcc.endTime - mlcc.startTime);
		
		//~~~~~~~~~~~~~~~~
		for(String linkid : mlcc.linkTrafficSet.keySet()){
			if(linkTrafficSet.get(linkid)== null){
				
				linkTrafficSet.put(linkid, new LinkAggTraffic(linkid, mlcc.linkTrafficSet.get(linkid).dur));
			
			}else {
				
				linkTrafficSet.get(linkid).dur += mlcc.linkTrafficSet.get(linkid).dur;
				
			}		
		}
	
		num++;
	}
	
}

class AggRegionCongestionCluster extends AggCongestionCluster{
	public String clusterID = null;
	
	public Envelope MBR;
	
	public int startTime = Integer.MAX_VALUE;
	public int endTime = Integer.MIN_VALUE;
	
	public int dur;
	
	public int num;
	
	public HashMap<String, MLinkAggTraffic> mLinkTrafficSet = new HashMap<String, MLinkAggTraffic>();
	
	public AggRegionCongestionCluster(String clusterID){
		this.clusterID  = clusterID;
	}
		
	public void add(RegionCongestionCluster regionCluster, Envelope mbr) {
		
		this.startTime = regionCluster.startTime < this.startTime ? regionCluster.startTime : this.startTime;

		this.endTime = regionCluster.endTime > this.endTime ? regionCluster.endTime :  this.endTime;
		
		//MBR扩展
		if(MBR == null)
			this.MBR = mbr;
		else
			this.MBR.expandToInclude(mbr);
				
		//聚集
		dur += (regionCluster.endTime - regionCluster.startTime);
		
		//形成对MultiLink的概要描述
		for(String mLinkID : regionCluster.mLinkTrafficSet.keySet()){
			
			MLinkAggTraffic mlAggTraffic = this.mLinkTrafficSet.get(mLinkID);
			
			MLinkAggTraffic mlAggTraffic_region = regionCluster.mLinkTrafficSet.get(mLinkID); //新加入的Region簇里面的MLinkTraffic
			
			if(mlAggTraffic == null){
				
			    mLinkTrafficSet.put(mLinkID, new MLinkAggTraffic(mLinkID, mlAggTraffic_region.startOffset, mlAggTraffic_region.endOffset, mlAggTraffic_region.startTime, mlAggTraffic_region.endTime));
			    
			} else {
				
            	mlAggTraffic.startOffset = mlAggTraffic_region.startOffset < mlAggTraffic.startOffset ? mlAggTraffic_region.startOffset : mlAggTraffic.startOffset;
		
		        mlAggTraffic.endOffset = mlAggTraffic_region.endOffset > mlAggTraffic.endOffset ? mlAggTraffic_region.endOffset : mlAggTraffic.endOffset;
		    
            	mlAggTraffic.startTime = mlAggTraffic_region.startTime < mlAggTraffic.startTime ? mlAggTraffic_region.startTime : mlAggTraffic.startTime;
        		
		        mlAggTraffic.endTime = mlAggTraffic_region.endTime > mlAggTraffic.endTime ? mlAggTraffic_region.endTime : mlAggTraffic.endTime;
		
		        mlAggTraffic.dur += mlAggTraffic_region.dur; //作了简化处理
		    }
		}
			
		num++;
		
	}
	
}




